/* Copyright (c) 2025 Beijing Semidrive Technology Corporation
 * SPDX-License-Identifier: Apache-2.0
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
/** *****************************************************************************************************
 * \file     Mcu_PllDrv.c                                                                            *
 * \brief    AUTOSAR 4.3.1 MCAL Mcu Driver                                                             *
 *                                                                                                      *
 * <table>                                                                                              *
 * <tr><th>Date           <th>Version                                                                   *
 * <tr><td>2023/08/02     <td>1.0.0                               *
 * </table>                                                                                             *
 *******************************************************************************************************/
/********************************************************************************************************
 *                                      Include header files                                            *
 *******************************************************************************************************/
#include "Platform_Types.h"
#include "Mcu_PllDrv.h"
#include "Mcu_Cfg.h"

#define MCU_START_SEC_CODE
#include "Mcu_MemMap.h"
/********************************************************************************************************
 *                                  Global Function Declarations                                        *
 *******************************************************************************************************/
/** *****************************************************************************************************
 * \brief pll enable
 * .
 *
 * \verbatim
 * Syntax             : Std_ReturnType Mcu_Ip_PllDrvEnable(const Mcu_ClkNodeType *clkPtr)
 *
 * Service ID[hex]    : None
 *
 * Sync/Async         : Synchronous
 *
 * Reentrancy         : Non reentrant
 *
 * Parameters (in)    : clkPtr - Pointer to clock node
 *
 * Parameters (inout) : None
 *
 * Parameters (out)   : None
 *
 * Return value       : errID
 *
 * Description        : pll enable
 * .
 * \endverbatim
 *******************************************************************************************************/
Std_ReturnType Mcu_Ip_PllDrvEnable(const Mcu_ClkNodeType *clkPtr)
{
    Std_ReturnType errStatus;

    if (CKGEN_PLL_CTRL_TYPE == clkPtr->type)
    {
        errStatus = Mcu_Ip_PllVcoEnable(clkPtr->base);
    }
    else
    {
        errStatus = MCU_E_PARAM_CONFIG;
    }

    return errStatus;
}

/** *****************************************************************************************************
 * \brief pll disable
 * .
 *
 * \verbatim
 * Syntax             : Std_ReturnType Mcu_Ip_PllDrvDisable(const Mcu_ClkNodeType *clkPtr)
 *
 * Service ID[hex]    : None
 *
 * Sync/Async         : Synchronous
 *
 * Reentrancy         : Non reentrant
 *
 * Parameters (in)    : clkPtr - Pointer to clock node
 *
 * Parameters (inout) : None
 *
 * Parameters (out)   : None
 *
 * Return value       : errID
 *
 * Description        : pll disable
 * .
 * \endverbatim
 *******************************************************************************************************/
Std_ReturnType Mcu_Ip_PllDrvDisable(const Mcu_ClkNodeType *clkPtr)
{
    Std_ReturnType errStatus = E_OK;

    if (CKGEN_PLL_CTRL_TYPE == clkPtr->type)
    {
        Mcu_Ip_PllVcoDisable(clkPtr->base);
    }
    else
    {
        errStatus = MCU_E_PARAM_CONFIG;
    }

    return errStatus;
}

/** *****************************************************************************************************
 * \brief pll frequency prepare
 * .
 *
 * \verbatim
 * Syntax             : Std_ReturnType Mcu_Ip_PllDrvPrepare(const Mcu_ClkNodeType *clkPtr, Mcu_PllRateType vcoRate,
 *                      Mcu_PllRateType ck0Rate, Mcu_PllRateType ck1Rate, const Mcu_PllSpreadConfigType *spreadConfig)
 *
 * Service ID[hex]    : None
 *
 * Sync/Async         : Synchronous
 *
 * Reentrancy         : Non reentrant
 *
 * Parameters (in)    : clkPtr - Pointer to clock node
 *                      vcoRate - vco rate to set
 *                      ck0Rate - pll ck0 output rate to set
 *                      ck1Rate - pll ck1 output rate to set
 *                      spreadConfig - Pointer to spread config
 *
 * Parameters (inout) : None
 *
 * Parameters (out)   : None
 *
 * Return value       : errID
 *
 * Description        : pll frequency prepare
 * .
 * \endverbatim
 *******************************************************************************************************/
Std_ReturnType Mcu_Ip_PllDrvPrepare(const Mcu_ClkNodeType *clkPtr, Mcu_PllRateType vcoRate,
                                    Mcu_PllRateType ck0Rate, Mcu_PllRateType ck1Rate, const Mcu_PllSpreadConfigType *spreadConfigPtr)
{
    Std_ReturnType errStatus;
    /* PRQA S 0310 1 */
    const Mcu_PllNodeType *pllPtr = (const Mcu_PllNodeType *)clkPtr;
    uint32 parentRate;

    if (CKGEN_PLL_CTRL_TYPE == pllPtr->clkNode.type)
    {
        errStatus = Mcu_Ip_PllVcoPrepare(pllPtr->clkNode.base, CKGEN_24M_RATE, vcoRate, ck0Rate, ck1Rate,
                                         spreadConfigPtr);
    }
    else if (CKGEN_PLL_CLK_TYPE == pllPtr->clkNode.type)
    {
        parentRate = Mcu_Ip_PllGetVcoRate(pllPtr->clkNode.base, CKGEN_24M_RATE);

        if (PLL_ID_NUM_VCO_CLK_0 == pllPtr->clkNode.id)
        {
            errStatus = Mcu_Ip_PllCkPrepare(pllPtr->clkNode.base, parentRate, ck0Rate, pllPtr->clkNode.id);
        }
        else if (PLL_ID_NUM_VCO_CLK_1 == pllPtr->clkNode.id)
        {
            errStatus = Mcu_Ip_PllCkPrepare(pllPtr->clkNode.base, parentRate, ck1Rate, pllPtr->clkNode.id);
        }
        else
        {
            errStatus = MCU_E_PARAM_CONFIG;
        }

    }
    else
    {
        errStatus = MCU_E_PARAM_CONFIG;
    }

    return errStatus;
}

/** *****************************************************************************************************
 * \brief pll frequency get
 * .
 *
 * \verbatim
 * Syntax             : Mcu_ClkRateType Mcu_Ip_PllDrvGetRate(Mcu_ClkNodeType *clkPtr)
 *
 * Service ID[hex]    : None
 *
 * Sync/Async         : Synchronous
 *
 * Reentrancy         : Non reentrant
 *
 * Parameters (in)    : clkPtr - Pointer to clock node
 *
 * Parameters (inout) : None
 *
 * Parameters (out)   : None
 *
 * Return value       : pll frequency
 *
 * Description        : pll frequency get
 * .
 * \endverbatim
 *******************************************************************************************************/
Mcu_ClkRateType Mcu_Ip_PllDrvGetRate(const Mcu_ClkNodeType *clkPtr)
{
    Mcu_PllRateType rate;
    /* PRQA S 0310 1 */
    const Mcu_PllNodeType *pllPtr = (const Mcu_PllNodeType *)clkPtr;
    uint32 parentRate;

    if (CKGEN_PLL_CTRL_TYPE == clkPtr->type)
    {
        rate = Mcu_Ip_PllGetVcoRate(clkPtr->base, CKGEN_24M_RATE);
    }
    else if (CKGEN_PLL_CLK_TYPE == clkPtr->type)
    {
        parentRate = Mcu_Ip_PllGetVcoRate(pllPtr->parent->base, CKGEN_24M_RATE);
        rate = Mcu_Ip_PllGetCkRate(clkPtr->base, clkPtr->id, parentRate);
    }
    else
    {
        rate =  UINT32_MAX;
    }

    return rate;
}


/** *****************************************************************************************************
 * \brief pll check locked.
 *
 * \verbatim
 * Syntax             : uint8 Mcu_Ip_PllDrvIsLocked(const Mcu_ClkNodeType *clkPtr)
 *
 * Service ID[hex]    : None
 *
 * Sync/Async         : Synchronous
 *
 * Reentrancy         : Non reentrant
 *
 * Parameters (in)    : clkPtr - Pointer to clock node
 *
 * Parameters (inout) : None
 *
 * Parameters (out)   : None
 *
 * Return value       : pll lock state
 *
 * Description        : get the pll lock state.
 *                      return 1 means in lock, 0 means unlock.
 *
 * \endverbatim
 *******************************************************************************************************/
uint8 Mcu_Ip_PllDrvIsLocked(const Mcu_ClkNodeType *clkPtr)
{
    uint8 pllLocked;

    pllLocked = Mcu_Ip_PllIsLocked(clkPtr->base);

    return pllLocked;
}

#define MCU_STOP_SEC_CODE
#include "Mcu_MemMap.h"
/* End of file */
